package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartUserMapper;
import com.sky.result.Result;
import com.sky.service.ShoppingCartUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class ShoppingCartUserServiceImpl implements ShoppingCartUserService {

    @Autowired
    private ShoppingCartUserMapper shoppingCartUserMapper;

    @Autowired
    DishMapper dishMapper;

    @Autowired
    SetmealMapper setmealMapper;

    /**
     * 添加
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        ShoppingCart dbshoppingCart = shoppingCartUserMapper.findShoppingCartByUserAndOptions(shoppingCart);
        // 如果查到的为null说明没有添加过,我们就开始添加,如果添加了就直接数量+1
        if (dbshoppingCart == null) {
            Long dishId = shoppingCart.getDishId();
            // 添加(之前的口味,dishId,setmealId都添加过了)
            if (dishId != null) {
                // dishId不会null说明为菜品
                Dish dish = dishMapper.findById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                // 否则就添加套餐
                Setmeal setmeal = setmealMapper.findById(shoppingCart.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartUserMapper.save(shoppingCart);
        } else {
            // 修改
            dbshoppingCart.setNumber(dbshoppingCart.getNumber() + 1);
            shoppingCartUserMapper.updateNumberById(dbshoppingCart);
        }
    }


    /**
     * 查询购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        // 查询之前获取id
        Long userId = BaseContext.getCurrentId();
        return shoppingCartUserMapper.list(userId);

    }

    /**
     * 清空
     */
    @Override
    public void clean() {
        Long userId = BaseContext.getCurrentId();
        shoppingCartUserMapper.clean(userId);
    }


    /**
     * 根据id删除购物车菜品
     *
     * @param shoppingCartDTO
     */

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        Long userId = BaseContext.getCurrentId();
        shoppingCart.setUserId(userId);
        shoppingCart = shoppingCartUserMapper.findShoppingCartByUserAndOptions(shoppingCart);
        Integer number = shoppingCart.getNumber();
        if (number <= 1) {
            // 根据id直接删除
            shoppingCartUserMapper.delShoppingCartById(shoppingCart.getId());
        } else {
            // 修改number-1
            shoppingCart.setNumber(shoppingCart.getNumber() - 1);
            shoppingCartUserMapper.updateNumberById(shoppingCart);
        }
    }

}
